/*   
 * Project: OSMP
 * FileName: PanelController.java
 * version: V1.0
 */
package com.osmp.web.panel.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.osmp.web.base.core.SystemConstant;
import com.osmp.web.base.core.SystemGlobal;
import com.osmp.web.panel.service.LoginService;
import com.osmp.web.user.entity.SysPrivilege;
import com.osmp.web.user.entity.User;
import com.osmp.web.user.service.SysPrivilegeService;
import com.osmp.web.user.service.UserService;
import com.osmp.web.utils.Md5Encode;
import com.osmp.web.utils.Utils;

/**
 * Description:
 *
 * @author: wangkaiping
 * @date: 20141:21:02
 */
@Controller
@RequestMapping("/panel")
public class PanelController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SysPrivilegeService pservice;

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public Object login(@RequestParam(value = "username", defaultValue = "", required = false) String username,
                        @RequestParam(value = "password", defaultValue = "", required = false) String password,
                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (Utils.isEmpty(username) || Utils.isEmpty(password)) {
            result.put("statusCode", 500);
            return result;
        }
        Map<String, Object> map = loginService.login(request, username, Md5Encode.encodeByMD5(password).toUpperCase());
        boolean temp = (boolean) map.get("flag");
        if (!temp) {// 
            result.put("statusCode", 500);
            return result;
        } else {
            User user = (User) map.get("LoginUser");
            SystemGlobal.setLoginUser(user);
        }
        result.put("statusCode", 200);
        result.put("referer", "/panel/main.do");
        return result;

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        Object object = session.getAttribute(SystemConstant.LOGIN_USER);
        if (object != null) {
            try {
                session.removeAttribute(SystemConstant.LOGIN_USER);
            } catch (Exception e) {
                object = null;
            }
        }
        return "redirect:../login.jsp";
    }

    @RequestMapping("/main")
    public ModelAndView main() {
        ModelAndView mav = new ModelAndView();
        User user = SystemGlobal.currentLoginUser();
        if (null != user) {
            List<SysPrivilege> menuList =  userService.selectUserPrivilegeByLevel(user.getId(), -1);
            mav.addObject("menuList", menuList);
        }else{
            mav.setViewName("redirect:/");
        }
        return mav;
    }

    /**
     * 
     *
     * @return
     */
    @RequestMapping("/menu")
    @ResponseBody
    public Object left(@RequestParam(value = "menuId") int menuId) {
        User user = SystemGlobal.currentLoginUser();
        List<SysPrivilege> menuList = userService.selectUserPrivilegeByLevel(user.getId(), menuId);
        List<Map<String,Object>> list = new ArrayList<>();
        for(SysPrivilege i : menuList){
            Map<String,Object> map = new HashMap<>();
            map.put("id", i.getId());
            map.put("pid", i.getPid());
            map.put("text", i.getName());
            if(i.getId() > 1000){
            	map.put("state", "open");
            }else{
            	map.put("state", "closed");
            }
            map.put("iconCls", i.getIcon());
            map.put("url", i.getUri());
            list.add(map);
        }
        return list;
    }

    @RequestMapping("/north")
    public ModelAndView north() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("username", "admin");
        return mav;
    }

    @RequestMapping("/welcome")
    public void welcome() {

    }

}
